﻿@*
  PropertyTranslation.cshtml
  Part of the Polyglot package.
*@
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines;
@{

    string selectedLanguage = RenderPage("SelectedLanguage.cshtml").ToString().Trim();

    DynamicNode page = Model;

    var contentFound = false;

    if (page.HasProperty(Parameter.Property + "_" + selectedLanguage) && !string.IsNullOrEmpty(page.GetProperty(Parameter.Property + "_" + selectedLanguage).ToString()))
    {
        contentFound = true;
        @Html.Raw(umbraco.library.RenderMacroContent(page.GetProperty(Parameter.Property + "_" + selectedLanguage).ToString(), page.Id))
    }

    if (!contentFound)
    {
        foreach (DynamicNode child in page.GetChildrenAsList)
        {
            if (child.NodeTypeAlias == page.NodeTypeAlias + "_TranslationFolder")
            {
                foreach (DynamicNode translation in child.GetChildrenAsList)
                {
                    var language = translation.GetProperty("language");

                    if (language != null && language.ToString().ToLower() == selectedLanguage.ToLower())
                    {
                        if (translation.HasProperty(Parameter.Property) && !string.IsNullOrEmpty(translation.GetProperty(Parameter.Property).ToString()))
                        {
                            contentFound = true;
                            @Html.Raw(umbraco.library.RenderMacroContent(translation.GetProperty(Parameter.Property).ToString(), translation.Id))
                        }
                    }
                }
            }
        }
    }

    if (!contentFound)
    {

        if (page.HasProperty(Parameter.Property) && !string.IsNullOrEmpty((page.GetProperty(Parameter.Property).ToString())))
        {
            @Html.Raw(umbraco.library.RenderMacroContent(page.GetProperty(Parameter.Property).ToString(), page.Id))
        }
    }
}